<?php 
require_once 'BaseMySQLDAO.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../Entity/Domain/PageDomain.php';
class PageDAO extends BaseMySQLDAO
{
	public function __construct($context) {
		parent::__construct($context, PageDomain::$CollectionName);
	}

	public function create($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "INSERT INTO page(id,user_id,category_id,code,title_en,title_de,title_nl,title_fr,content_en,content_de,content_nl,content_fr,page_order,status,created_datetime,modified_datetime)
					VALUES	(
								".$obj->getId().",
								".$obj->getUser_id().",
								".$obj->getCategory_id().",
								'".$obj->getCode()."',
								'".$obj->getTitle_en()."',
								'".$obj->getTitle_de()."',
								'".$obj->getTitle_nl()."',
								'".$obj->getTitle_fr()."',
								".$obj->getContent_en().",
								".$obj->getContent_de().",
								".$obj->getContent_nl().",
								".$obj->getContent_fr().",
								".$obj->getPage_order().",
								'".$obj->getStatus()."',
								'".$obj->getCreated_datetime()."',
								'".$obj->getModified_datetime()."'
							)";
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function deleteByOid($oid){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "DELETE FROM page WHERE id=".$oid;
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function update($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "UPDATE page
				SET
					page.user_id = ".$obj->getUser_id().",
					page.category_id = ".$obj->getCategory_id().",
					page.code = '".$obj->getCode()."',
					page.title_en = '".$obj->getTitle_en()."',
					page.title_de = '".$obj->getTitle_de()."',
					page.title_nl = '".$obj->getTitle_nl()."',
					page.title_fr = '".$obj->getTitle_fr()."',
					page.content_en = ".$obj->getContent_en().",
					page.content_de = ".$obj->getContent_de().",
					page.content_nl = ".$obj->getContent_nl().",
					page.content_fr = ".$obj->getContent_fr().",
					page.page_order = ".$obj->getPage_order().",
					page.status = '".$obj->getStatus()."',
					page.created_datetime = '".$obj->getCreated_datetime()."',
					page.modified_datetime = '".$obj->getModified_datetime()."'
				 WHERE id=".$obj->getId();
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function findByOid($oid)
	{
		$pageDomain = null;
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM page WHERE page.id = ".$oid;
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		if($res)
		{
			$pageDomain = new PageDomain($res);
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $pageDomain;
	}

	public function findAll()
	{
		$pageDomainArr = array();
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM page";
		$res = $this->getMySQLConnection()->mysqlData($inSQL);
		if($res)
		{
			while ($data = mysql_fetch_assoc($res))
			{
				$pageDomain = new PageDomain($data);
				$pageDomainArr[] = $pageDomain;
			}
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $pageDomain;
	}

}
?>